package com.atguigu.tingshu.search.receiver;

import cn.hutool.core.util.ObjectUtil;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;


     //监听上架专辑信息
     @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value =    @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true"),
            key= MqConst.ROUTING_ALBUM_UPPER
    ))
    public void upperAlbum(Long albumId, Message message, Channel channel){
        log.info("[搜索服务]监听到上架专辑消息{}",albumId);
        if(ObjectUtil.isNotNull(albumId)){
            searchService.upperAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    //监听删除专辑
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value=@Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true"),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    public void lowerAlbum(Long albumId,Message message,Channel channel){
         log.info("[搜索服务]监听到专辑下架消息:{}",albumId);
         if(ObjectUtil.isNotNull(albumId)){
             searchService.lowerAlbum(albumId);
         }
         channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
